package com.interview.ujiuye1;

/**
 * Copyright (C), 2018-2020
 * FileName: Demo2
 * Author:   kongfanyu
 * Date:     2020/9/14 11:07
 */
public class Demo2 {
    public static void main(String[] args) {
        int[] arr = {1,3,6,9,12,23,33,44,45};
        int res = binarySearch(arr, 44);
        System.out.println("res=" + res);

    }
    /**
     * 二分查找
     */
    public static int binarySearch(int[] arr, int findElem) {
        int low = 0;
        int high = arr.length - 1;
        int mid;
        while (low <= high) {
            mid = (low + high) / 2;

            if (findElem < arr[mid]) {
                high = mid - 1;
            }

            if (findElem > arr[mid]) {
                low = mid + 1;
            }
            if (arr[mid] == findElem) {
                return mid;
            }
        }
        return -1;
    }
    private static void test1() {
        String name = "迪丽热巴";
        char [] ch = name.toCharArray();
        for (int i = ch.length -1; i >=0 ; i--) {
            System.out.print(ch[i]);
        }
        System.out.println();
        StringBuffer buffer = new StringBuffer(name);
        StringBuffer buffer1 = buffer.reverse();
        System.out.println(buffer1);
    }
}
